package com.library.controller;

import com.library.dto.BookDTO;
import com.library.entity.Book;
import com.library.service.BookService;
import com.library.vo.PageVO;
import com.library.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public ResultVO<PageVO<Book>> getBooks(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) String category
    ) {
        try {
            PageVO<Book> result = bookService.getBookPage(current, size, title, author, category);
            return ResultVO.success(result);
        } catch (Exception e) {
            log.error("查询书籍失败: {}", e.getMessage());
            return ResultVO.error("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResultVO<Book> getBookById(@PathVariable Long id) {
        try {
            Book book = bookService.getBookById(id);
            if (book != null) {
                return ResultVO.success(book);
            } else {
                return ResultVO.error("书籍不存在");
            }
        } catch (Exception e) {
            log.error("查询书籍详情失败: {}", e.getMessage());
            return ResultVO.error("查询失败: " + e.getMessage());
        }
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResultVO<String> addBook(@RequestBody BookDTO bookDTO) {
        try {
            Book book = new Book();
            book.setTitle(bookDTO.getTitle());
            book.setAuthor(bookDTO.getAuthor());
            book.setPublisher(bookDTO.getPublisher());
            book.setPublishDate(bookDTO.getPublishDate());
            book.setIsbn(bookDTO.getIsbn());
            book.setCategory(bookDTO.getCategory());
            book.setDescription(bookDTO.getDescription());
            book.setCoverImage(bookDTO.getCoverImage());
            book.setTotalQuantity(bookDTO.getTotalQuantity());

            boolean result = bookService.addBook(book);
            if (result) {
                return ResultVO.success("添加成功");
            } else {
                return ResultVO.error("添加失败");
            }
        } catch (Exception e) {
            log.error("添加书籍失败: {}", e.getMessage());
            return ResultVO.error("添加失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResultVO<String> updateBook(@PathVariable Long id, @RequestBody BookDTO bookDTO) {
        try {
            Book book = new Book();
            book.setId(id);
            book.setTitle(bookDTO.getTitle());
            book.setAuthor(bookDTO.getAuthor());
            book.setPublisher(bookDTO.getPublisher());
            book.setPublishDate(bookDTO.getPublishDate());
            book.setIsbn(bookDTO.getIsbn());
            book.setCategory(bookDTO.getCategory());
            book.setDescription(bookDTO.getDescription());
            book.setCoverImage(bookDTO.getCoverImage());
            book.setTotalQuantity(bookDTO.getTotalQuantity());

            boolean result = bookService.updateBook(book);
            if (result) {
                return ResultVO.success("更新成功");
            } else {
                return ResultVO.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新书籍失败: {}", e.getMessage());
            return ResultVO.error("更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResultVO<String> deleteBook(@PathVariable Long id) {
        try {
            boolean result = bookService.deleteBook(id);
            if (result) {
                return ResultVO.success("删除成功");
            } else {
                return ResultVO.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除书籍失败: {}", e.getMessage());
            return ResultVO.error("删除失败: " + e.getMessage());
        }
    }
}
